/*
 * Service class 
 * Created on 6 juin 2013 ( Time 15:03:52 )
 */

package org.demo.typeabonnement.service;

import java.util.List;

import org.demo.typeabonnement.bean.Typeabonnement;
import org.demo.typeabonnement.dao.JpaTypeabonnementDAO;
import org.telosys.starterkits.strutsjpa.EntityManagerHelper;
import org.telosys.starterkits.strutsjpa.IServices;
import javax.persistence.PersistenceException;

import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;


public class TypeabonnementServices implements IServices<Typeabonnement, Integer> {

	protected final Logger LOG = LoggerFactory.getLogger(TypeabonnementServices.class);

	public Typeabonnement load(final Integer id) {
		if (LOG.isDebugEnabled()) LOG.debug("load");
		Typeabonnement typeabonnement;
		try {
			EntityManagerHelper.beginTransaction();
			JpaTypeabonnementDAO typeabonnementDAO = new JpaTypeabonnementDAO();
			typeabonnement = typeabonnementDAO.findById(id);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return typeabonnement ;
	}

	public Typeabonnement save(final Typeabonnement entity) {
		if (LOG.isDebugEnabled()) LOG.debug("save");
		Typeabonnement entityNew;
		try {
			EntityManagerHelper.beginTransaction();
			JpaTypeabonnementDAO typeabonnementDAO = new JpaTypeabonnementDAO();
			entityNew = typeabonnementDAO.update(entity);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return entityNew;
	}

	public void delete(final Integer id) {
		if (LOG.isDebugEnabled()) LOG.debug("delete");
		try {
			EntityManagerHelper.beginTransaction();
			JpaTypeabonnementDAO typeabonnementDAO = new JpaTypeabonnementDAO();
			typeabonnementDAO.delete(id);
			EntityManagerHelper.commitAndCloseEntityManager();	
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
	}

	public List<Typeabonnement> search(final Typeabonnement typeabonnement) {
		if (LOG.isDebugEnabled()) LOG.debug("search");
		List<Typeabonnement> liste;
		try {
			EntityManagerHelper.beginTransaction();
			JpaTypeabonnementDAO typeabonnementDAO = new JpaTypeabonnementDAO();
			liste = typeabonnementDAO.search(typeabonnement);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return liste;
	}

}
